package com.abel.manager.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.abel.domain.AccountInfo;
import com.abel.manager.RegistrationManager;
import com.abel.manager.exception.RegistrationException;
import com.abel.repository.AuthRepository;
import com.abel.repository.exception.DuplicateUserException;
import com.abel.repository.exception.RepositoryException;

public class RegistrationManagerImpl implements RegistrationManager{

	protected final Log logger = LogFactory.getLog(getClass());
	
	private AuthRepository authRepository;
	
	@Override
	public int registerApplicant(AccountInfo accountInfo) throws RegistrationException, DuplicateUserException {

		logger.debug("@RegistrationManagerImpl : registerApplicant : entered");
		
		try {
			
			int userId = this.authRepository.addRegistration(accountInfo);

			//if user id is valid, log the user in
			if (userId == 0) {
				throw new RegistrationException("Registration for user - "+ accountInfo.getUserName() +  " failed.");
			}
			return userId;
		
		}
		
		catch (RepositoryException e) {
			
			e.printStackTrace();
			throw new RegistrationException("Exception while registering applicant",e);
		
		}
	}


	@Override
	public void setAuthRepository(AuthRepository authRepository) {
		this.authRepository = authRepository;
		
	}
	
	
	

}
